<template>
  <div class="app-container">
    <div style="color: #c0c4cc">
      直播管理
    </div>
    <div
      style="font-size: 14px; margin-bottom: 20px;display: flex; flex-wrap: wrap;align-content:space-evenly;margin-left: 5%">
      <div class="my-card" v-for="item in authorList" :key="item.id"
           :style="{
       'background-color': getBackgroundColor(item),
       'border-radius': '5px',
       'color': '#606266',
       'margin-bottom': '24px',
       'width': '22%',
       'height': '12%',
       'margin-right': '40px'
     }">
        <div style="display: flex;justify-content: space-between">
          <div style="display:flex;flex-direction: column;">
            <div style="margin-top: 6px; margin-left: 10px;font-size: 14px;font-weight: bold">
              {{ item.name }}
            </div>
            <div style="margin-top: 6px;margin-left: 10px;font-size: 12px">
              {{ item.uid }}
            </div>
            <div style="margin-top: 4px; margin-bottom: 6px; margin-left: 10px; font-size: 12px;
             border: 1px solid #ddd; padding: 1px; border-radius: 3px;">
              今日已播 <span style="color: #46a6ff">{{ item.daySum }}</span> H / 本月已播 <span
              style="color: #46a6ff">{{ item.monthSum }}
            </span> H
            </div>
          </div>

          <div style="display:flex;flex-direction: column;">
            <div style="margin-top: 6px; margin-right: 6px">
              <el-button icon="el-icon-video-pause"
                         v-if="liveStatus1List.some(ab => ab.doudianAuthorId == item.id && ab.manageId == $store.state.user.id) "
                         v-hasRole="['zhubo','quanNengZhuBo']" @click="downLive(item)" size="small" type="warning">下播
              </el-button>

              <span v-else-if="liveStatus1List.some(ab => ab.doudianAuthorId == item.id)">
               <i class="el-icon-user-solid" style="color: #46a6ff"></i>
                  <span style="color: #46a6ff">
                         {{ liveStatus1List.find(ab => ab.doudianAuthorId == item.id).nickName }}
                  </span>
                </span>
              <el-button icon="el-icon-video-play" effect="dark" v-else v-hasRole="['zhubo','quanNengZhuBo']"
                         @click="upLive(item)"
                         size="small" type="primary">上播
              </el-button>
            </div>

            <div style="margin-top: 6px; margin-right: 6px">
              <el-button icon="el-icon-video-pause" v-show="liveStatus1List.some(ab => ab.doudianAuthorId == item.id)"
                         v-hasRole="['admin','doudianyunying']" @click="forceDownLive(item)" size="small" type="danger">
                强下
              </el-button>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div>
      <div style="color: #c0c4cc;margin-bottom: 20px">
        直播记录
      </div>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item>
          <el-select v-model="queryParams.status" placeholder="直播状态" filterable collapse-tags clearable>
            <el-option label="直播中" value="1"></el-option>
            <el-option label="直播结束" value="2"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-select v-model="queryParams.doudianAuthorId" filterable collapse-tags clearable placeholder="直播账号">
            <el-option v-for="item in authorList" :key="item.id" :value="item.id" :label="item.name"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-select v-model="queryParams.manageId" placeholder="主播" filterable collapse-tags clearable>
            <el-option v-for="item in anchorList" :key="item.userId" :value="item.userId"
                       :label="item.nickName"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker v-model="queryParams.liveTime" style="width: 360px" value-format="yyyy-MM-dd HH:mm:ss"
                          :default-time="['00:00:00', '23:59:59']"
                          type="datetimerange" range-separator="-" start-placeholder="上播开始日期"
                          end-placeholder="上播结束日期">
          </el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!--      <el-row :gutter="10" class="mb8" v-hasRole="['admin']">-->
      <!--        <el-col :span="1.5">-->
      <!--          <el-button-->
      <!--            type="primary"-->
      <!--            plain-->
      <!--            icon="el-icon-plus"-->
      <!--            size="mini"-->
      <!--            @click="handleAdd"-->
      <!--            v-hasPermi="['hmk:liveRecords:add']"-->
      <!--          >新增-->
      <!--          </el-button>-->
      <!--        </el-col>-->
      <!--        <el-col :span="1.5">-->
      <!--          <el-button-->
      <!--            type="success"-->
      <!--            plain-->
      <!--            icon="el-icon-edit"-->
      <!--            size="mini"-->
      <!--            :disabled="single"-->
      <!--            @click="handleUpdate"-->
      <!--            v-hasPermi="['hmk:liveRecords:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--        </el-col>-->
      <!--        <el-col :span="1.5">-->
      <!--          <el-button-->
      <!--            type="danger"-->
      <!--            plain-->
      <!--            icon="el-icon-delete"-->
      <!--            size="mini"-->
      <!--            :disabled="multiple"-->
      <!--            @click="handleDelete"-->
      <!--            v-hasPermi="['hmk:liveRecords:remove']"-->
      <!--          >删除-->
      <!--          </el-button>-->
      <!--        </el-col>-->
      <!--        <el-col :span="1.5">-->
      <!--          <el-button-->
      <!--            type="warning"-->
      <!--            plain-->
      <!--            icon="el-icon-download"-->
      <!--            size="mini"-->
      <!--            @click="handleExport"-->
      <!--            v-hasPermi="['hmk:liveRecords:export']"-->
      <!--          >导出-->
      <!--          </el-button>-->
      <!--        </el-col>-->
      <!--        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
      <!--      </el-row>-->

      <el-table v-loading="loading" :data="liveRecordsList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="直播账号" align="center" prop="authorName">
          <template #default="{row}">
            <span v-html="liveAuthorNameToStr(row) "></span>
          </template>
        </el-table-column>
        <el-table-column label="主播" align="center" prop="nickName"/>


        <el-table-column label="直播时间">
          <template #default="{row}">
            <span v-html="liveTimeToStr(row) "></span>
          </template>
        </el-table-column>


        <el-table-column label="直播时长(分钟)" align="center" prop="duration"/>
        <el-table-column label="直播状态" align="center" prop="status">
          <template #default="{row}">
          <span :class="getStatusClass(row.status)">
            {{ liveStatusToStr(row.status) }}
          </span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="time">
          <template #default="{row}">
          <span>
            {{ formatTimeTitle(row.time) }}
          </span>
          </template>
        </el-table-column>

      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改直播管理对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="直播编号" prop="serial">
            <el-input v-model="form.serial" placeholder="请输入直播编号"/>
          </el-form-item>
          <el-form-item label="主播id" prop="manageId">
            <el-input v-model="form.manageId" placeholder="请输入主播id"/>
          </el-form-item>
          <el-form-item label="直播账号id" prop="doudianAuthorId">
            <el-input v-model="form.doudianAuthorId" placeholder="请输入直播账号id"/>
          </el-form-item>
          <el-form-item label="直播开始时间" prop="startTime">
            <el-input v-model="form.startTime" placeholder="请输入直播开始时间"/>
          </el-form-item>
          <el-form-item label="直播结束时间" prop="endTime">
            <el-input v-model="form.endTime" placeholder="请输入直播结束时间"/>
          </el-form-item>
          <el-form-item label="直播时长(存分钟数)" prop="duration">
            <el-input v-model="form.duration" placeholder="请输入直播时长(存分钟数)"/>
          </el-form-item>
          <el-form-item label="添加时间(也就是直播开始时间)" prop="time">
            <el-input v-model="form.time" placeholder="请输入添加时间(也就是直播开始时间)"/>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>

    </div>

  </div>
</template>

<script>
import {
  addLiveRecords,
  authorListBySum,
  delLiveRecords,
  getLiveRecords,
  listLiveRecords,
  selectUserByRoleId,
  status1List,
  updateLiveRecords
} from "@/api/hmk/liveRecords";
import {formatTimeTitle, hasRole} from "@/utils/hjf-common";


export default {
  name: "LiveRecords",
  data() {
    return {
      liveStatus1List: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 直播管理表格数据
      liveRecordsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serial: null,
        manageId: null,
        doudianAuthorId: null,
        startTime: null,
        endTime: null,
        duration: null,
        status: null,
        time: null,
        liveTime: [],
      },
      authorList: [],
      anchorList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
    this.status1List();
    this.authorListBySum();
    this.selectUserByRoleId();
  },
  methods: {
    getBackgroundColor(item) {
      const liveStatus = this.liveStatus1List.find(ab => ab.doudianAuthorId == item.id);
      if (!liveStatus) {
        return '#e1f3d8';
      }
      if (liveStatus.manageId == this.$store.state.user.id) {
        return '#ffcc99';
      }
      return '#f0f0f0';
    }
    ,
    upLive(item) {
      this.$confirm('确定要上播【' + item.name + '】吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        addLiveRecords({doudianAuthorId: item.id}).then(response => {
          this.$modal.msgSuccess("上播成功");
          this.getList();
          this.status1List();
        });
      }).catch(() => {
        this.$message({type: 'info', message: '已取消'});
      });
    },

    downLive(item) {
      this.$confirm('确定要下播【' + item.name + '】吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateLiveRecords({doudianAuthorId: item.id}).then(response => {
          this.$modal.msgSuccess("下播成功");
          this.getList();
          this.status1List();
        });
      }).catch(() => {
        this.$message({type: 'info', message: '已取消'});
      });
    },
    forceDownLive(item) {
      this.$confirm('确定要强制下播【' + item.name + '】吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateLiveRecords({doudianAuthorId: item.id}).then(response => {
          this.$modal.msgSuccess("强制下播成功");
          this.getList();
          this.status1List();
        });
      }).catch(() => {
        this.$message({type: 'info', message: '已取消'});
      });
    },

    hasRole,
    liveTimeToStr(row) {
      if (row.endTime) {
        return '开始: ' + formatTimeTitle(row.startTime) + '<br>' + '结束: ' + formatTimeTitle(row.endTime);
      } else {
        return '开始: ' + formatTimeTitle(row.startTime) + '<br>' + '结束: --';
      }
    },
    liveAuthorNameToStr(row) {
      if (null != row) {
        return row.authorName + '<br>' + row.uid;
      }
    },
    liveStatusToStr(status) {
      if (status == 1) {
        // #23cfe1
        return '直播中';
      } else if (status == 2) {
        return '直播结束';
      }
    },

    getStatusClass(status) {
      return {
        'live-status--active':
          status == 1
      }
    },
    status1List() {
      status1List().then(response => {
        this.liveStatus1List = response.rows;
      });
    },
    formatTimeTitle,
    /** 查询直播管理列表 */
    getList() {
      this.loading = true;
      listLiveRecords(this.queryParams).then(response => {
        this.liveRecordsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    }
    ,
    /** 获取主播列表 */
    selectUserByRoleId() {
      selectUserByRoleId().then(response => {
        this.anchorList = response.data;
      });
    },


    authorListBySum() {
      authorListBySum({pageNum: 1, pageSize: 20}).then(response => {
        this.authorList = response.rows;
      });
    }

    ,
    /** 查询直播账号管理列表 */
    getAuthorList() {
      authorListBySum({pageNum: 1, pageSize: 20}).then(response => {
        this.authorList = response.rows;
      });
    }
    ,
// 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    }
    ,
// 表单重置
    reset() {

      this.form = {
        id: null,
        serial: null,
        manageId: null,
        doudianAuthorId: null,
        startTime: null,
        endTime: null,
        duration: null,
        status: null,
        time: null
      };
      this.resetForm("form");
    }
    ,
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    }
    ,
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.manageId = null,
        this.queryParams.doudianAuthorId = null,
        this.queryParams.status = null,
        this.queryParams.liveTime = [],
        this.resetForm("queryForm");
      this.handleQuery();
    }
    ,
// 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    }
    ,
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加直播管理";
    }
    ,
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLiveRecords(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改直播管理";
      });
    }
    ,
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateLiveRecords(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLiveRecords(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    }
    ,
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除直播管理编号为"' + ids + '"的数据项？').then(function () {
        return delLiveRecords(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    }
    ,
    /** 导出按钮操作 */
    handleExport() {
      this.download('hmk/liveRecords/export', {
        ...this.queryParams
      }, `liveRecords_${new Date().getTime()}.xlsx`)
    }
  }
}
;
</script>
<style scoped lang="scss">
.live-status--active {
  color: #46a6ff;
}

/* 自定义阴影 */
.my-card {
  box-shadow: 0 2px 6px 0 rgba(0, 0, 0, 0.1) !important;
}

/* 悬停时更深的阴影 */
.my-card:hover {
  box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.2) !important;
}
</style>
